package April._23.work7;

public class Main {
    private static final Object OBJECT=new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (OBJECT) {
                    for (int i = 1; i < 100; i+=2) {
                        System.out.println(Thread.currentThread().getName()+" " + i);
                        OBJECT.notifyAll();
                        try {
                            OBJECT.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    OBJECT.notifyAll();
                }

            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (OBJECT) {
                    for (int i = 2; i <=100; i+=2) {
                        System.out.println(Thread.currentThread().getName()+" "+ i);
                        OBJECT.notifyAll();
                        try {
                            OBJECT.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    //必须要加、否则会有一个线程一直在睡觉、没有人去唤醒她；
                    //好伤心哦、被人遗忘了、呜呜呜呜呜呜呜
                    OBJECT.notifyAll();
                }

            }
        });

        t1.start();
        Thread.sleep(50);
        t2.start();
    }
}
